package com.glodon.paas.document.web.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glodon.paas.document.web.util.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

@Controller
@RequestMapping("/")
public class HomeController {

    public static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class);

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView root(HttpServletRequest request, HttpServletResponse response) throws ServletException,
                                                                                      IOException {
        return index(request, response);
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws ServletException,
                                                                                       IOException {
        if (SecurityUtils.getCurrentUser() == null) {
            request.getRequestDispatcher("api/sso/signin").forward(request, response);
            return null;
        }
        return new ModelAndView(new RedirectView("file/index"));
    }
}
